<?php

/**
*	Admin Controller
*
*	This controller is the main brains behind the application, any of the submission, updating, or management is done through here.
*	This controller is the only one that uses the administer model, but also calls a few functions from display to reduce redundancy.
*	
*/
class Admin extends Controller {
	
	/**
	*	default function
	*
	*	This function loads when /admin/ is called with no other arguments and loads the base admin panel
	*
	*/
	function index() {
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		// If they're asking for this without a cookie then they shouldnt be here, if so redirect them.
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		
		$data['list'] = $this->Administer->admin_home_list(20);
		
		$this->load->view('admin',$data);
	}
	
	/**
	*	Function: login
	*
	*	This function loads the login page and checks/prepares the credentials to be sent to the administer model.
	*	If the login is successful it also sets the cookie for the administrator.
	*
	*/
	function login() {
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			$this->load->library('form_validation');
			
			$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
			$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
			
			if ($this->form_validation->run() == TRUE) {
				
				$this->load->model('Administer');
				
				$login = $this->Administer->login($_POST['username'],$_POST['password']);
				
				if ($login != 0){
					// I used to store more information in the cookie but CI does wierd things with cookies that I didnt have time to debug
					$cookie = array(
					   'name'   => 'Review',
					   'value'  => $login->display,
					   'expire' => '3600',
					   'domain' => '.nocturnalgenesis.net',
					   'path'   => '/',
				   );
	
					set_cookie($cookie);
					redirect('/admin/');
					
				} else {
					// Bad login info, reload the login view (with errors, if I actually sent the errors!)
					$this->load->view('login');
					
				}
				
				
			} else {
				// info didnt pass the forum validation, should send errors here.
				$this->load->view('login');
			}
		} else {
			// Successful login, send them in
			redirect('/admin/');
		}
		
	}
	
	/**
	*	Function: logout
	*
	*	This function in theory should kill the cookie and redirect the administrator to the public side, CI doesnt like to delete
	*	the cookie sometimes though.
	*
	*/
	function logout() {
		$this->load->helper('url');
		$this->load->helper('cookie');
		
		delete_cookie("Review");
		redirect(site_url('review/'));
		
	}
	
	/**
	*	Function: post
	*
	*	This function loads the post view so that we can insert a new article into the database as well as checks the forum
	*	on submission for validity and then hands everything off to the administer model to be inserted.
	*
	*/
	function post() {
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		$this->load->library('form_validation');	
		
		$this->form_validation->set_rules('title', 'Article Title', 'required|xss_clean');
		$this->form_validation->set_rules('content', 'Article Content', 'required|xss_clean');
		
		if ($this->form_validation->run() == TRUE) {
			
			if (strcmp($_POST['topic'], 'Review') == 0) {
				
				// Check to see if its a series overview or a granular episode review
				if (empty($_POST['preview'])) {
					// Check for episode.
					if (empty($_POST['ep'])) {
						if (empty($_POST['final'])) {
							$e = FALSE;
						} else {
							$e = $_POST['final'];
						}
					} else {
						$e = $_POST['ep'];
					}
				} else {
					// Becuase i made the decision long ago to use 0 as a value this is a workaround to check its true value, isset() would work too
					$pre = $_POST['preview'];
					$e = 0;
				}
				
				if ($e || $pre) { // If everything's OK.
				
					$result = $this->Administer->insert_review(get_cookie('Review'),$_POST['topic'],$_POST['title'],$_POST['content'],$_POST['series'],$_POST['ep']);
					
					if($result) {
						redirect(site_url('admin/'));
					}

						
				}
					
			} else { // Post is NOT a review

				$result = $this->Administer->insert_post(get_cookie('Review'),$_POST['topic'],$_POST['title'],$_POST['content']);
				
				if($result) {
					redirect(site_url('admin/'));
				}

			}
			
			
			
		} else {
			// This is either a first load or the article didnt pass validation so reload the view.
			$data['titles'] = $this->Administer->get_seriestitle();
			$this->load->view('post', $data);
		}
		
	}
	
	/**
	*	Function: add_Series
	*
	*	This function loads the add_series view and checks the form for validity to add a new series to the database.
	*	
	*
	*/
	function add_series(){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		$this->load->library('form_validation');	
		
		$this->form_validation->set_rules('series', 'Series Name', 'required|xss_clean');
		$this->form_validation->set_rules('studio', 'Studio Name', 'required|xss_clean');
		$this->form_validation->set_rules('eps', 'Series Length', 'required|integer|xss_clean');
		$this->form_validation->set_rules('genre', 'Genre', 'required|xss_clean');
		
		if ($this->form_validation->run() == TRUE) {
			
			// Becuase of the way the shows are aired, theres no way you could sort the seasons correctly without additional info like this
			$num = $_POST['season'];	
			switch ($num) {
				case 1:
					$sea = 'Winter';
					break;
				case 2:
					$sea = 'Spring';
					break;
				case 3:
					$sea = 'Summer';
					break;
				case 4:
					$sea = 'Fall';
					break;
				default:
					break;
			}
			
			$result = $this->Administer->insert_series($_POST['series'],$_POST['studio'],$_POST['season'],$sea,$_POST['year'],$_POST['eps'],$_POST['genre']);
			
			if($result) {
				redirect(site_url('admin/'));
			}
			
		} else {
			// First load or bad input
			$this->load->view('add_series');
		}
	}
	
	/**
	*	Function: edit
	*
	*	This function loads the edit_list view, which links into the specific edit pages for whatever item you click on.
	*	$cat is used to detemine if articles or series get listed, $id is used to navigate the results
	*	
	*	@param string $cat
	*	@param integer $id
	*/
	function edit($cat,$id){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		
		$data['list'] = $this->Administer->edit_list($cat,$id);
		// Need to pass it on to format the links
		$data['cat'] = $cat;
		$data['id'] = $id;
		
		$this->load->view('edit', $data);
	}
	
	/**
	*	Function: edit_post
	*
	*	This function loads the edit_post view which enables you to update an article that has been posted previously.
	*	$id is used to specify the specific article to edit
	*	
	*	@param integer $id
	*/
	function edit_post($id){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Display');
		
		$data['post'] = $this->Display->get_article($id);
		$data['id'] = $id;		
		
		$this->load->model('Administer');
		
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('type', 'Article Type', 'required|alpha|xss_clean');
		$this->form_validation->set_rules('series', 'Series ID', 'integer|xss_clean');
		$this->form_validation->set_rules('ep', 'Episode value', 'integer|xss_clean');
		$this->form_validation->set_rules('title', 'Article Title', 'required|xss_clean');
		$this->form_validation->set_rules('content', 'Article Content', 'required|xss_clean');
		
		
		if ($this->form_validation->run() == TRUE) {
			
			if (strcmp($_POST['type'], 'Review') == 0) {
				
				// Check to see if its a series overview or a granular episode review
				if (!isset($_POST['ep'])) {
					// Check for episode.
					$e = FALSE;
				} else if ($_POST['ep'] == 0) {
					$pre = TRUE;
					$e = 0;
				} else {
					$e = $_POST['ep'];
				}
				
				if (empty($_POST['series'])) {
					$s = FALSE;
				} else {
					$s = $_POST['series'];
				}
				
				if (($e || $pre) && $s) { // If everything's OK.
				
					$result = $this->Administer->update_review($id,$_POST['type'],$_POST['title'],$_POST['content'],$_POST['series'],$_POST['ep']);
					
					if($result) {
						redirect(site_url('admin/'));
					}

						
				} // if we failed its missing ep or series, needs error report
					
			} else { // Post is NOT a review

				$result = $this->Administer->update_post($id,$_POST['type'],$_POST['title'],$_POST['content']);
				
				if($result) {
					redirect(site_url('admin/'));
				}

			}
			
			
			
		} else {
			$this->load->view('edit_post', $data);
		}
		
	}
	
	/**
	*	Function: edit_series
	*
	*	This function loads the edit_series view which enables you to update an existing series
	*	$id is used to specify the specific series to edit
	*	
	*	@param integer $id
	*/
	function edit_series($id){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Display');
		
		$data['series'] = $this->Display->get_series($id);
		$data['id'] = $id;
		
		$this->load->model('Administer');
		
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('series', 'Series Name', 'required|xss_clean');
		$this->form_validation->set_rules('studio', 'Studio Name', 'required|xss_clean');
		$this->form_validation->set_rules('s_num', 'Season Number', 'required|integer|xss_clean');
		$this->form_validation->set_rules('season', 'Season', 'required|xss_clean');
		$this->form_validation->set_rules('year', 'Year', 'required|integer|xss_clean');
		$this->form_validation->set_rules('eps', 'Length', 'required|integer|xss_clean');
		$this->form_validation->set_rules('genre', 'Genre', 'required|xss_clean');
		
		
		if ($this->form_validation->run() == TRUE) {
				
			$result = $this->Administer->update_series($id,$_POST['series'],$_POST['studio'],$_POST['s_num'],$_POST['season'],$_POST['year'],$_POST['eps'],$_POST['genre']);
			
			if($result) {
				redirect(site_url('admin/'));
			}

		} else {
			$this->load->view('edit_series', $data);
		}
		
	}
	
	/**
	*	Function: edit_schedule
	*
	*	This function loats the edit_schedule view which allows editing of the list of shows that are currently broadcasting.
	*	Makes use of AJAX outside of the CI view to update the list without reloading the page.
	*	Somewhat useless as I havent ever figured out a good way for it to update the additional information (aired eps), for 
	*	the time being this changes the broadcast flag and the begin and end dates depending on when the show is added or removed.
	*	
	*/
	function edit_schedule(){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		
		$data['list'] = $this->Administer->broadcast_list();
		
		$this->load->view('edit_schedule',$data);
	}
	
	/**
	*	Function: list_series
	*
	*	Loads a view with series ID's and names to easily identify if a series has been added, or to look up a series ID.
	*	
	*/
	function list_series(){
		$this->load->helper('url');
		$this->load->model('Administer');
		
		$data['list'] = $this->Administer->series_list();
		
		$this->load->view('list_series',$data);		
	}
	
	/**
	*	Function: manage_list
	*
	*	Loads the manage_list view which has links set to deal with the image functions related to a review article
	*	$id is used to navigate the results displayed
	*
	*	@param integer $id	
	*/
	function manage_list($id){
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->model('Administer');
		
		$data['list'] = $this->Administer->manage_list($id);
		$data['id'] = $id;
		
		$this->load->view('manage_list',$data);
	}
	
	/**
	*	Function: manage
	*
	*	Loads the manage view with data about the directory structure assigned to the article, uses FTP to create a folder tree
	*	based on the arguments given, which are the username, series id, and episode. Loops through the directory structure and
	*	creates directories if required or will retrieve the file path for the folder to be used when it is determined to exist.
	*	$pid is used to notify which specific article to update as the seires/ep combo would not be unique enough.
	*
	*	@param integer $series
	*	@param integer $ep
	*	@param integer $pid
	*/
	function manage($series,$ep,$pid){
		$this->load->helper('form');
		$this->load->helper('cookie');
		$this->load->helper('url');
		
		if (!get_cookie('Review')){
			redirect(site_url('review/'));
			exit();
		}
		
		$this->load->library('ftp');
		$this->ftp->connect();
		
		// This shows the calculations needed with substr to limit the information to only what we need
		//
		// FSU/LIS4368/final/review_images/ (32) + $un_size / +(1) + $series length / +(1) + $ep length
		$test = FALSE;
		$un_size = strlen(get_cookie('Review'));
		$s_size = strlen($series);
		$e_size = strlen($ep);
		
		// List the base directory and check for the user's directory
		$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/');
		foreach($dirlist as $row){
			$tempstring = substr($row,32);
			if(strcmp($tempstring,get_cookie('Review')) == 0){
				$test = TRUE;
				break;
			}				
		}
		if(!$test){
			$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/');
			// User Directory not found, create it
			$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/');
			foreach($dirlist as $row){
				$teststring = substr($row,(32+$un_size+1));
				if(strcmp($teststring,$series) == 0){
					// Series directory found, continuing
					$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
					foreach($dirlist as $row){
						$teststring = substr($row,(32+$un_size+1+$s_size+1));
						if(strcmp($teststring,$ep) == 0){
							// Episode directory found, continuing
							// This is as deep as we need, so get the path
							$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
							$test = TRUE;
							break;
						} 
					}
					if(!$test) {
						// Episode directory not found, create it
						$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
						// This is as deep as we need, so get the path
						$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
						$test = TRUE;
					}
				} 
			}
			if(!$test) {
				// Series directory not found, create it
				$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
				$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
				foreach($dirlist as $row){
					$teststring = substr($row,(32+$un_size+1+$s_size+1));
					if(strcmp($teststring,$ep) == 0){
						// Episode directory found, continuing
						// This is as deep as we need, so get the path
						$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
						$test = TRUE;
						break;
					}
				}
				if(!$test) {
					// Episode directory not found, create it
					$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
					// This is as deep as we need, so get the path
					$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
					$test = TRUE;
				}
			}
		} else {
			$test = FALSE;
			// User directory found, continue
			$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/');
			foreach($dirlist as $row){
				$teststring = substr($row,(32+$un_size+1));
				if(strcmp($teststring,$series) == 0){
					// Series directory found, continue
					$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
					foreach($dirlist as $row){
						$teststring = substr($row,(32+$un_size+1+$s_size+1));
						if(strcmp($teststring,$ep) == 0){
							// Episode directory found, continue
							// This is as deep as we need, so get the path
							$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
							$test = TRUE;
							break;
						}
					}
					if(!$test){
						// Episode directory not found, create it
						$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
						// This is as deep as we need, so get the path
						$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
						$test = TRUE;
					}
					
					break;
				}
			}
			if(!$test) {
				// Series directory not found, create it
				$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
				$dirlist = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/');
				foreach($dirlist as $row){
					$teststring = substr($row,(32+$un_size+1+$s_size+1));
					if(strcmp($teststring,$ep) == 0){
						// Episode directory found, continue
						// This is as deep as we need, so get the path
						$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
						$test = TRUE;
						break;
					} 
				}
				if(!$test) {
					// Episode directory not found, create it
					$this->ftp->mkdir('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
					// This is as deep as we need, so get the path
					$data['updir'] = 'review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/';
					$test = TRUE;
				}
			}
		}
		
		// List the final directory to get any files that exist so we can display them
		$data['list'] = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
		$this->ftp->close();
		
		$data['series'] = $series;
		$data['ep'] = $ep;
		$data['pid'] = $pid;
		$this->load->view('manage',$data);
		
	}
	
	/**
	*	Function: make_thumbs
	*
	*	Based on the FTP information we got from manage, we'll go ahead and loop through all the files in our directory
	*	and make thumbnails automatically. We even check so we dont thumbnail an image thats already a thumbnail.
	*	
	*	This function works on small images, but due to PHP Memory limits bigger images or a large set of images will terminate
	*	the script.
	*	
	*	$pid is used to notify which specific article to update as the seires/ep combo would not be unique enough.
	*
	*	@param integer $series
	*	@param integer $ep
	*	@param integer $pid
	*/
	function make_thumbs($series,$ep,$pid){
		$this->load->helper('cookie');
		$this->load->helper('url');
		$this->load->library('ftp');
		$this->load->library('image_lib');
		
		$this->ftp->connect();
		$list = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
		$this->ftp->close();
		
		$cnt = 0;
		foreach($list as $image){
			// We use this as a quick way to skip /. and /.. which are always returned by the FTP list
			if($cnt > 1) {
				$str = substr($image,18);
				$parts = explode('/',$str);
				if(!is_numeric(strpos($parts[4],'.png')) && !is_numeric(strpos($parts[4],'.jpg')) && !is_numeric(strpos($parts[4],'.gif'))){
					/*Not an image we recognize, in reality this could potentially end up being wrong 
						if the filename contains multiple image type strings. stripos() really should be used for case
						insensitivity but thats new to PHP5 and I dont have time to go error check everything under PHP5.*/
				} else {
					// Check to see if its already an image thumbnail so we dont create needless copies
					if(!is_numeric(strpos($parts[4],'_thumb'))){
						$config['image_library'] = 'GD2';
						$config['source_image'] = $str;
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = TRUE;
						$config['width'] = 192;
						$config['height'] = 108;
						$this->image_lib->initialize($config);
						if (!$this->image_lib->resize())
							{
								echo $this->image_lib->display_errors();
							}
						$this->image_lib->clear();
					}
				}
			}
			$cnt++;
		}
		
		// Reload the main manage view which will display the updated folder contents automatically
		redirect(site_url('admin/manage/'.$series.'/'.$ep.'/'.$pid));
	}
	
	/**
	*	Function: insert_images
	*
	*	This function requests the contents of a review article, parses the contents for special flags that can be inserted
	*	by using the 'Block image' javacript button when submitting/editing a review, and will replace the flags with sets of
	*	images neatly calculated based on the FTP contents of the article directory and how many image block flags it finds.
	*	
	*	
	*	$pid is used to notify which specific article to update as the seires/ep combo would not be unique enough.
	*
	*	@param integer $series
	*	@param integer $ep
	*	@param integer $pid
	*/
	function insert_images($series,$ep,$id){
		$this->load->helper('cookie');
		$this->load->helper('url');
		$this->load->library('ftp');
		
		$this->ftp->connect();
		$list = $this->ftp->list_files('./FSU/LIS4368/final/review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
		$this->ftp->close();
		
		$cnt = 0;
		$t = 0;
		foreach($list as $image){
			if($cnt > 1) {
				// Only get the portion of the file path we care about to make our lives easier
				$str = substr($image,18);
				// Break it up so we can just grab the file name
				$parts = explode('/',$str);
				if(!is_numeric(strpos($parts[4],'.png')) && !is_numeric(strpos($parts[4],'.jpg')) && !is_numeric(strpos($parts[4],'.gif'))){
					/*Not an image we recognize, in reality this could potentially end up being wrong 
						if the filename contains multiple image type strings. stripos() really should be used for case
						insensitivity but thats new to PHP5 and I dont have time to go error check everything under PHP5.*/
				} else {
					if(is_numeric(strpos($parts[4],'_thumb'))){
						// Store the file name for later when we need to reinsert it
						$tlist[$t] = $parts[4];
						$t++;
					}
				}
			}
			$cnt++;
		}
		
		$this->load->model('Administer');
		
		$cont = $this->Administer->get_content($id);
		
		// IF we were able to request the article contents, continue
		if($cont){
			$icount = 0;
			// Preformat the URLs to be used with the images for ease of use
			$url = site_url('review_images/'.get_cookie('Review').'/'.$series.'/'.$ep.'/');
			// Parse the article contents for image block tags
			$blocks = substr_count($cont->content,'##');
			if($blocks > 0){
				for($b = 0; $b < $blocks; $b++){
					// Set up the array with all the flags so this works properly
					$arbl[$b] = '##'.$b.'#';
				}
				if($t % $blocks == 0){
					// There are no 'odd' images that cant be assigned evenly to the ammount of blocks we have so just keep going
					$blk_size = $t / $blocks;
					for($i = 0; $i < $blocks; $i++){
						$rep[$i] = '<span class="center">';
						for($k = 0; $k < $blk_size; $k++){
							$tmp = $url.'/'.$tlist[$icount];
							$tmp = str_replace('_thumb','',$tmp);
							$rep[$i] .= "<a href=\"$tmp\"><img src=\"".$url.'/'.$tlist[$icount]."\" alt=\"alt here\"/></a>";
							$icount++;
						}
						$rep[$i] .= '</span>';
					}
				} else {
					// We have a remainder, so take the largest whole number to be used for all blocks, and we'll add 'odd' images to
					// the last block
					$blk_size = (integer)($t / $blocks);
					for($i = 0; $i < $blocks; $i++){
						$rep[$i] = '<span class="center">';
						if($i == ($blocks -1)){
							// This is the last block so we have to make sure we get all the rest of the images
							for($icount; $icount < $t; $icount++){
								$tmp = $url.'/'.$tlist[$icount];
								$tmp = str_replace('_thumb','',$tmp);
								$rep[$i] .= "<a href=\"$tmp\"><img src=\"".$url.'/'.$tlist[$icount]."\" alt=\"alt here\"/></a>";
							}							
						} else {
							// Normal block, do as usual
							for($k = 0; $k < $blk_size; $k++){
								$tmp = $url.'/'.$tlist[$icount];
								$tmp = str_replace('_thumb','',$tmp);
								$rep[$i] .= "<a href=\"$tmp\"><img src=\"".$url.'/'.$tlist[$icount]."\" alt=\"alt here\"/></a>";
								$icount++;
							}
						}
						$rep[$i] .= '</span>';
					}					
				}
				// Replace the flags with our new image blocks
				$new_cont = str_replace($arbl,$rep,$cont->content);
				// And update the article with the new contents
				$q = $this->Administer->update_post_with_images($id,$new_cont);
			}
		}
		
		if(isset($q)){
			redirect(site_url('review/article/'.$id));
		} else {
			redirect(site_url('admin/edit_post/'.$id));
		}
	}
	
	/**
	*	Function: do_upload
	*
	*	Function used with the form to upload images, we have hidden values in the form to give the extra information for the
	*	correct directory. I would have loved to have a multiple upload solution but that seemed not to be something native to CI.
	*
	*/
	function do_upload(){
		$this->load->helper('cookie');
		$this->load->helper('url');
		$this->load->library('upload');
		
		$config['upload_path'] = 'review_images/'.get_cookie('Review').'/'.$_POST['series'].'/'.$_POST['ep'].'/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '2048';
		
		$this->upload->initialize($config);
		
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
			$this->load->view('admin', $error);
		}	
		else
		{
			$data = array('upload_data' => $this->upload->data());
			
			redirect(site_url('admin/manage/'.$_POST['series'].'/'.$_POST['ep'].'/'.$_POST['pid']));
		}
		
	}
	
}
?>